﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FaultDiagnosisSystem.Models.ViewModels;
using FaultDiagnosisSystem.Shared;
using System.Data.SqlClient;

namespace FaultDiagnosisSystem.DAL
{
    public class RealTimeFaultDataDaily
    {
        /// <summary>
        /// 获取实时故障每日分析数据的最新日期,无数据则返回默认时间
        /// </summary>
        /// <returns></returns>
        internal DateTime GetLastDay()
        {
            using (Models.DataModels.TurbineFaultContext context=new Models.DataModels.TurbineFaultContext())
            {
                var daily = context.RealTimeFaultDataDaily
                    .OrderByDescending(d => d.FaultDataDailyId)
                    .FirstOrDefault();
                if (daily!=null)
                {
                    return daily.Date;
                }
                else
                {
                    return Global.DefaultTime;
                }
            }
        }

        internal IList<RealTimeFaultDataDailyViewModel> GetListOfLastDay()
        {
            using (Models.DataModels.TurbineFaultContext context = new Models.DataModels.TurbineFaultContext())
            {
                var list = context.Database.SqlQuery<RealTimeFaultDataDailyViewModel>(@"EXEC p_GetComponentFaultTotalTimeOfLastDay")
                    .ToList();
                return list;
            }
        }

        /// <summary>
        /// 获取指定日期的RealTimeFaultDataDailyViewModel集合
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        internal IList<RealTimeFaultDataDailyViewModel> GetList(DateTime day)
        {
            using (Models.DataModels.TurbineFaultContext context=new Models.DataModels.TurbineFaultContext())
            {
                SqlParameter param = new SqlParameter("@date", day);
                var list = context.Database.SqlQuery<RealTimeFaultDataDailyViewModel>(@"EXEC dbo.p_GetComponentFaultTotalTime @date",param)
                    .ToList();
                return list;
            }
        }
    }
}